package com.example.demo.demos.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: lixz
 * @Date: 2024/08/01/19:36
 */
@Configuration
public class ElasticSearchClientConfig {

    @Value("${elasticsearch.url}")
    private String esUrl = "";

    private static final Logger log = LoggerFactory.getLogger(ElasticSearchClientConfig.class);

    @Bean
    public ElasticsearchClient elasticsearchClient() {
        log.info("esurl:{}", esUrl);
        // Create the low-level client
        RestClient restClient = RestClient.builder(
                new HttpHost(esUrl, 9200)).build();

        // Create the transport with a Jackson mapper
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        // And create the API client
        ElasticsearchClient esClient = new ElasticsearchClient(transport);
        return esClient;
    }

}
